﻿using System.Linq;
using System.Web;
using System.Web.Services;
using RedRiver.Framework.Web.Models;

namespace RedRiver.Framework.Web.Services{
    /// <summary>
    ///UserInfoService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tjredriver.com.cn/UserInfoService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
 [System.Web.Script.Services.ScriptService]
    public class UserInfoService : WebService{
       
        public static Employee GetEmployeeInfo(HttpContext context){
            if (context.Session["EmployeeInfo"] == null){
                context.Session["EmployeeInfo"] = GetEmployeeInfo(context.User.Identity.Name);
            }
            return (Employee) context.Session["EmployeeInfo"];
        }

        [WebMethod(Description = "根据工号获取员工信息")]
        public Employee GetEmployee(string employCode){
            using (var context = new EmployeesDataContext())
            {
                return context.Employee.Single(e => e.EmployeeCode == employCode);
            }
        }

        public static Employee GetEmployeeInfo(string userName){
            using (var context = new EmployeesDataContext()){
                if ((userName.Length > 5) && (userName.StartsWith("EP"))){
                    var employeeCode = userName.Substring(2);
                    return context.Employee.Single(e => e.EmployeeCode == employeeCode);
                }
                return context.Employee.Single(e => e.UserName == userName);
            }
        }

        [WebMethod(Description = "获得当前登录用户信息", EnableSession = true)]
        public Employee GetCurrentUser(){
            return GetEmployeeInfo(Context.User.Identity.Name);
        }

        [WebMethod(Description = "获得当前登录用户ID", EnableSession = true)]
        public string GetCurrentUserName(){
            return Context.User.Identity.Name;
        }
    }
}